package com.lx.spring;

import com.lx.spring.biz.UserBiz;
import com.lx.spring.mapper.UserMapper;
import com.lx.spring.web.UserAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;


@Configuration
@ComponentScan
public class AnnoConfig {

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @Lazy
    @Scope("prototype") //原型模式 看不到张三的销毁方法
    public Demo demo(){
        Demo demo = new Demo();
        demo.setName("张三");
        return demo;
    }
    @Bean(initMethod = "init", destroyMethod = "destroy")
    @Primary //主bean 默认true
    @DependsOn("demo2")
    public Demo demo1(){
        Demo demo = new Demo();
        demo.setName("李四");
        return demo;
    }
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public Demo demo2(){
        Demo demo = new Demo();
        demo.setName("王五");
        return demo;
    }

    // @Autowired 表示从IOC容器中获取该bean 注入到该属性中
    @Autowired
    UserMapper userMapper;


    @Bean
    public UserBiz userBiz(){
        UserBiz userBiz = new UserBiz();
        userBiz.setUserMapper(userMapper);
        return userBiz;
    }


    @Bean
    public UserMapper userMapper(){
        return new UserMapper();
    }

    // Bean 方法参数对象, Spring 会从IOC容器获取一个对应的bean 注入进来
    //这个叫"接口(方法)注入"
    @Bean
    public UserAction userAction(UserBiz userBiz){
        UserAction userAction = new UserAction();
        userAction.setUserBiz(userBiz);
        return userAction;
    }


}
